package com.bjb.vr.gateway.exception;

import com.bjb.vr.common.exception.BusinessException;
import com.bjb.vr.common.exception.CommonException;
import com.bjb.vr.common.exception.DaoException;
import com.bjb.vr.common.exception.RpcException;
import com.bjb.vr.common.result.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description: 全局异常处理类
 * @author: HuJingBo
 * @time: 2022/2/27 22:09
 */
@Slf4j
@RestControllerAdvice
public class ApplicationExceptionHandler {

    /**
     * 处理 Exception 异常
     *
     * @param e 异常
     * @return 处理结果
     */
    @ExceptionHandler(Exception.class)
    public CommonResult handlerException(Exception e) {
        log.error(e.getMessage(), e);
        return CommonResult.Fail("系统异常,请联系管理员！");
    }

    /**
     * 处理空指针异常
     *
     * @param e 异常
     * @return 处理结果
     */
    @ExceptionHandler(NullPointerException.class)
    public CommonResult handlerNullPointerException(NullPointerException e) {
        log.error(e.getMessage(), e);
        return CommonResult.Fail("空指针异常,请及时处理！");
    }

    /**
     * 处理通用自定义异常
     *
     * @param e 异常
     * @return 处理结果
     */
    @ExceptionHandler(CommonException.class)
    public CommonResult handlerCommonException(CommonException e) {
        log.error(e.getMessage(), e);
        return CommonResult.Fail(e.getCode().toString(), e.getMessage());
    }

    /**
     * 处理业务操作自定义异常
     *
     * @param e 异常
     * @return 处理结果
     */
    @ExceptionHandler(BusinessException.class)
    public CommonResult handlerBusinessException(BusinessException e) {
        log.error(e.getMessage(), e);
        return CommonResult.Fail(e.getCode().toString(), e.getMessage());
    }

    /**
     * 处理数据操作自定义异常
     *
     * @param e 异常
     * @return 处理结果
     */
    @ExceptionHandler(DaoException.class)
    public CommonResult handlerDaoException(DaoException e) {
        log.error(e.getMessage(), e);
        return CommonResult.Fail(e.getCode().toString(), e.getMessage());
    }

    /**
     * 处理远程调用操作自定义异常
     *
     * @param e 异常
     * @return 处理结果
     */
    @ExceptionHandler(RpcException.class)
    public CommonResult handlerRpcException(RpcException e) {
        log.error(e.getMessage(), e);
        return CommonResult.Fail(e.getCode().toString(), e.getMessage());
    }
}
